<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{ active: isLogin }">
        <div class="big-contain" v-if="isLogin">
          <div class="btitle">账户登录</div>
          <div class="bform">
            <input type="text" placeholder="用户名" v-model="form.username" />
            <input type="password" placeholder="密码" v-model="form.password" />
          </div>
          <button class="bbutton" @click="login">登录</button>
        </div>
        <div class="big-contain" v-else>
          <div class="btitle">创建账户</div>
          <div class="bform">
            <input type="text" placeholder="用户名" v-model="form.username" />
            <input type="password" placeholder="密码" v-model="form.password" />
          </div>
          <button class="bbutton" @click="register">注册</button>
        </div>
      </div>
      <div class="small-box" :class="{ active: isLogin }">
        <!-- 注册 -->
        <div class="small-contain" v-if="isLogin">
          <div class="stitle">你好，朋友!</div>
          <p class="scontent">开始注册，成为宝可梦训练大师</p>
          <button class="sbutton" @click="changeType">注册</button>
        </div>
        <!-- 登录 -->
        <div class="small-contain" v-else>
          <div class="stitle">欢迎回来!</div>
          <p class="scontent">赶紧登录, 了解更多宝可梦信息</p>
          <button class="sbutton" @click="changeType">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { loginApi, registerAPi } from '../../api/auth'

const router = useRouter()
let isLogin = ref(true)
let form = reactive({
  username: '',
  password: ''
})

// 切换 登录/注册状态
function changeType() {
  isLogin.value = !isLogin.value
  form.username = ''
  form.password = ''
}

function login() {
  if (form.username === "" || form.password === "") {
    ElMessage.error("用户名和密码不能为空")
  } else {
    loginApi(form).then(res => {
      console.log(res.data)
      if (res.data.code === 2000) {
        ElMessage.success(res.data.msg)
        // 设置TOKEN
        sessionStorage.setItem('token', res.data.token)
        // 跳转到主页
        router.push('/home')
      } else if (res.data.code = 4000) {
        ElMessage.warning(res.data.msg)
      }
    }).catch(err => {
      ElMessage.error('服务器异常', err)
    })
  }
}

function register() {
  if (form.username === "" || form.password === "") {
    ElMessage.error("用户名和密码不能为空")
  } else {
    registerAPi(form).then(res => {
      console.log(res)
      if (res.data.code == 2000) {
        ElMessage.success(res.data.msg)
      } else {
        ElMessage.warning(res.data.msg)
      }
    })
    form.password = ''
    isLogin.value = !isLogin.value
  }
}
</script>

<style scoped>
.login-register {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
  background-image: url(./login-bg.jpg);
  background-size: cover;
}

.contain {
  opacity: 0.8;
  width: 60%;
  height: 60%;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0, 0 0 6px #f0f0f0;
}

.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  background: linear-gradient(rgb(230, 181, 181), rgb(157, 213, 235));
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}

.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.btitle {
  font-size: 1.5em;
  font-weight: bold;
  color: rgb(249, 102, 126);
}

.bform {
  width: 100%;
  height: 40%;
  padding: 2em 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

.bform .errTips {
  display: block;
  width: 50%;
  text-align: left;
  color: red;
  font-size: 0.7em;
  margin-left: 1em;
}

.bform input {
  width: 50%;
  height: 35px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}

.bbutton {
  width: 20%;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: rgb(57, 167, 176);
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.small-box {
  width: 30%;
  height: 100%;
  background: linear-gradient(135deg, rgb(31, 119, 127), rgb(163, 205, 192));

  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}

.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}

.scontent {
  font-size: 0.8em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}

.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

/* 滑动动作 */
.big-box.active {
  left: 0;
  transition: all 0.5s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
</style>